(ns lr.main
  (:require lr.image)
  (:use lr.scene lr.ray))

(defn ray-intsct-sphere
  ;; c: Center: 3-vector
  ;; r: Radius
  [ray c r color]
  (let [dir (:direction ray)
	a (dot dir dir)
	o-minus-c (v- (:origin ray) c)
	b (* 2 (dot o-minus-c dir))
	c (- (dot o-minus-c o-minus-c) (* r r))
	disc (- (* b b) (* 4 a c))]
    (if (or (< disc 0)
	    (= a 0))
      false
      color)))

      (comment ;;Else, return intersection pt
      (v+ (:origin ray)
	  (v*-scalar dir
		     (/ (- (java.lang.Math/sqrt disc) b)
			(* 2 a)))))
      
(defn create-sphere [c r color]
  (struct object #(ray-intsct-sphere % c r color)))


(println "Rendering...")
(time
 (render-scene
  (struct scene [(create-sphere [1,0,0] 1 [0 255 0])
		 (create-sphere [2,0,0] 1 [0 0 255])
		 (create-sphere [3,0,0] 1 [0 255 255])
		 (create-sphere [4,0,0] 1 [255 0 255])])))

(lr.image/write-out)



